package com.qf.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.DtsBrand;
import com.qf.pojo.DtsCategory;
import com.qf.service.CategoryService;
import com.qf.util.ResponseUtil;
import com.qf.vo.CatVo;
import com.sun.corba.se.spi.ior.ObjectKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
//解决前后端分离跨域访问问题
@CrossOrigin("*")
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 商品类目列表
     *
     * @param id
     * @param name
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @return
     */
    @RequestMapping("/list")
    public Object list(Integer id, String name,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @RequestParam(defaultValue = "add_time") String sort,
                       @RequestParam(defaultValue = "desc") String order) {


        IPage<DtsCategory> resultPage = categoryService.queryCategoryPage(id, name, page, limit, sort, order);

        List<DtsCategory> list = resultPage.getRecords();

        long total = resultPage.getTotal();
        Map<String, Object> data = new HashMap<>();

        data.put("total", total);
        data.put("items", list);

        return ResponseUtil.ok(data);

    }

    /**
     * 商品一级类目列表
     *
     * @return
     */
    @RequestMapping("/l1")
    public Object l1() {

        List<CatVo> list = categoryService.selectcatVo11();

        return ResponseUtil.ok(list);
    }

    @RequestMapping("/create")
    public Object create(@RequestBody DtsCategory category){

        if (category.getName()==null){
            throw new RuntimeException("名字不可以为空！！");
        }
        DtsCategory dtsCategory = categoryService.add(category);

        return ResponseUtil.ok(dtsCategory);
    }
    @RequestMapping("/update")
    public Object update(@RequestBody DtsCategory category){
        if (category.getName()==null){
            throw new RuntimeException("名字不可以为空！！");
        }
        return ResponseUtil.ok();
    }

    @RequestMapping("/delete")
    public Object delete(@RequestBody DtsCategory category){
        Integer id = category.getId();
        if (StringUtils.isEmpty(id)){
            return  ResponseUtil.badArgument();
        }
        categoryService.delete(id);
        return ResponseUtil.ok();

    }
}
